<template>
  <div class="app-container">
    <!-- 搜索筛选操作栏 -->
    <div class="filter-container clearfix">
      <el-input
        v-model="listQuery.code"
        placeholder="请输入账号"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.name"
        placeholder="请输姓名"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.status"
        placeholder="请选择状态"
        clearable
        style="width: 150px; margin-left: 10px"
        class="filter-item"
      >
        <el-option label="缴纳" :value="0" />
        <el-option label="扣除" :value="1" />
        <el-option label="闭店提现" :value="2" />
        <el-option label="退货退回" :value="3" />
        <el-option label="支付逾期" :value="4" />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        icon="iconfont qt-caozuo-xunhuan1"
        @click="clearFilter"
      >
        清除条件
      </el-button>
    </div>

    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      style="width: 100%"
    >
      <el-table-column label="账号" prop="user.user" align="center" />
      <el-table-column label="姓名" prop="user.name" align="center" />
      <el-table-column label="金额" prop="money" align="center" />
      <el-table-column label="状态" align="center">
        <template slot-scope="{ row }">
          {{ row.type | typeFilter }}
        </template>
      </el-table-column>
      <el-table-column label="保证金支付状态" align="center">
        <template slot-scope="{ row }">
          {{ row.pay_status | payStatusFilter }}
        </template>
      </el-table-column>
      <el-table-column label="商品支付状态" align="center">
        <template slot-scope="{ row }">
          {{ row.pro_pay_status | proPayStatusFilter }}
        </template>
      </el-table-column>
      <el-table-column label="关联拍品" header-align="center">
        <template slot-scope="{ row }">
          <el-image
            v-if="row.product"
            style="width: 40px; height: 40px; text-align: center"
            :src="row.product && row.product.image"
          />
          <span style="vertical-align: top; margin-left: 10px">{{
            row.product && row.product.name
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="参拍状态" align="center">
        <template slot-scope="{ row }">
          {{ row.attend_status | attendStatusFilter }}
        </template>
      </el-table-column>
      <el-table-column label="时间" prop="created_at" align="center" />
      <el-table-column label="操作" align="center" width="160px">
        <template slot-scope="{ row }">
          <!-- <el-button
            size="mini"
            plain
            type="success"
            @click="handleDeal(row.id, '缴纳', 0)"
            v-if="row.status > 4"
          >
            缴纳
          </el-button> -->
          <template v-if="row.pay_status == 1 && row.type === 0">
            <el-button
              size="mini"
              plain
              type="success"
              @click="handleDeal(row.id, '退回', 3)"
            >
              退回
            </el-button>
            <el-button
              size="mini"
              plain
              type="success"
              @click="handleDeal(row.id, '扣除', 1)"
            >
              扣除
            </el-button>
          </template>
          <span v-else>-</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getBuyersdepositList, updateBuyersdeposit } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'FinanceBuyersDeposit',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  filters: {
    //   0缴纳,1扣除,2提现（闭店）,3退货返回,4支付逾期
    typeFilter(val) {
      let result = ''
      switch (val) {
        case 0:
          result = '缴纳'
          break
        case 1:
          result = '扣除'
          break
        case 2:
          result = '提现'
          break
        case 3:
          result = '货返回'
          break
        case 4:
          result = '支付逾期'
          break
      }
      return result
    },
    // 0失败，1成功
    payStatusFilter(val) {
      let result = ''
      switch (val) {
        case 0:
          result = '失败'
          break
        case 1:
          result = '成功'
          break
      }
      return result
    },
    // 商品支付状态
    proPayStatusFilter(val) {
      let result = ''
      switch (val) {
        case 0:
          result = '未支付'
          break
        case 1:
          result = '已支付'
          break
      }
      return result
    },
    // attend_status（参拍状态）// 0领先,1出局 2未出价
    attendStatusFilter(val) {
      let result = ''
      switch (val) {
        case 0:
          result = '领先'
          break
        case 1:
          result = '出局'
          break
        case 2:
          result = '未出价'
          break
      }
      return result
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        per_page: 20,
        name: undefined,
        code: undefined,
        status: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    clearFilter() {
      this.listQuery['name'] = undefined
      this.listQuery['code'] = undefined
      this.listQuery['status'] = undefined
      this.handleFilter()
    },
    getList() {
      this.listLoading = true
      getBuyersdepositList(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 查看
    handleLook(row) {
      this.$router.push('/member/detail/' + row.id)
    },
    handleDeal(id, title, status) {
      this.$confirm('确认要将状态设置成' + title + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.listLoading = true
          updateBuyersdeposit(id, { type: status, source: 1 })
            .then(() => {
              this.$notify({
                title: '提示',
                message: title + '成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            })
            .catch(() => {
              this.listLoading = false
            })
        })
        .catch(() => {})
    }
  }
}
</script>
